package com.schoolvisitors.controller.user;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.common.PageResult;
import com.schoolvisitors.context.UserContext;
import com.schoolvisitors.dto.BorrowInstrumentFromListDto;
import com.schoolvisitors.dto.BorrowInstrumentFromUpDto;
import com.schoolvisitors.dto.ReturnInstrumentDto;
import com.schoolvisitors.entity.Admin;
import com.schoolvisitors.entity.InstrumentLoan;
import com.schoolvisitors.entity.User;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.AdminPermissionService;
import com.schoolvisitors.service.InstrumentService;
import com.schoolvisitors.vo.InstrumentLoanToUserVo;
import com.schoolvisitors.vo.InstrumentLoanVo;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/v1/user")
public class InstrumentLoanController {

    private final InstrumentService instrumentService;
    private final AdminPermissionService adminPermissionService;

    public InstrumentLoanController(InstrumentService instrumentService, AdminPermissionService adminPermissionService) {
        this.instrumentService = instrumentService;
        this.adminPermissionService = adminPermissionService;
    }

    @PostMapping("borrowInstrumentFromList")
    public ResultBody borrowInstrumentFromList(@RequestBody @Valid BorrowInstrumentFromListDto dto) {
        User user = UserContext.getUser();
        instrumentService.borrowInstrumentFromList(user, dto);
        return ResultBody.success();
    }

    @PostMapping("borrowInstrumentFromUp")
    public ResultBody borrowInstrumentFromUp(@RequestBody @Valid BorrowInstrumentFromUpDto dto){
        User user = UserContext.getUser();
        instrumentService.borrowInstrumentFromUp(user, dto);
        return ResultBody.success();
    }

    @GetMapping("/getInstrumentLoans")
    public ResultBody getUserInstrumentLoans(
            @RequestParam("type") String type,
            @RequestParam("page") int page,
            @RequestParam("size") int size
    ) {
        User user = UserContext.getUser();
        Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createAt"));
        Page<InstrumentLoan> pages = instrumentService.getLoansByUser(user, type, pageable);

        Page<InstrumentLoanVo> voPages = pages.map(InstrumentLoanVo::toVo);
        log.info("{}", pages);
        return ResultBody.success(PageResult.bind(voPages));
    }


    @PostMapping("returnInstrument")
    public ResultBody returnInstrument(@RequestBody ReturnInstrumentDto dto){
        User user = UserContext.getUser();
        instrumentService.returnInstrument(user, dto);
        return ResultBody.success();
    }

    @GetMapping("getLoanReviewers")
    public ResultBody getLoanReviewer(){

        List<Map<String, String>> reviewerList = new ArrayList<>();
        for (Admin admin : adminPermissionService.getInstrumentPermissionReviewers()) {
            Map<String, String> reviewerMap = new HashMap<>();
            reviewerMap.put("id", String.valueOf(admin.getId()));
            reviewerMap.put("name", admin.getRealName());
            reviewerList.add(reviewerMap);
        }

        return ResultBody.success(reviewerList);
    }


}
